<?php

namespace app\modules\Cruise\models;

use Yii;
use app\models\Supplier;

/**
 * This is the model class for table "cruise".
 *
 * @property integer $id
 * @property string $supplier_id
 * @property string $name
 * @property string $en_name
 * @property integer $from_city
 * @property string $weight
 * @property integer $num
 * @property integer $length
 * @property integer $width
 * @property integer $room
 * @property integer $ceil
 * @property integer $speed
 * @property string $first
 * @property string $desc
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class Cruise extends \yii\db\ActiveRecord
{
    public $year;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cruise';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['supplier_id'], 'required'],
            [['from_city', 'to_city', 'num', 'length', 'width', 'room', 'ceil', 'speed', 'status', 'user_id'], 'integer'],
            [['weight'], 'number'],
            [['first','renovation', 'created_at', 'updated_at'], 'safe'],
            [['supplier_id'], 'string', 'max' => 5],
            [['code'], 'string', 'max' => 8],
            [['name'], 'string', 'max' => 128],
            [['en_name'], 'string', 'max' => 255],
            [['desc'], 'string', 'max' => 1024],
        ];
    }


    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        return parent::beforeSave($insert);
    }//}}}

       
    // 保存后处理
    public function afterSave($insert, $changedAttributes){//{{{
    
    }//}}}


    //  获取对应供应商   
    public function getSupplier(){
        return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']);
    }

    //  获取舱房
//    public function getRoom(){
//        return $this->hasMany(Room::className(), ['cruise_id' => 'id']);
//    }

     // 获取对应图片
    public function getImage(){
        return  $this->hasMany(Image::className(),['product_id'=>'id']);
    }
    
    // 获取对应图片
    public function getImageOne(){
        return  $this->hasOne(Image::className(),['product_id'=>'id'])->onCondition(['image.type'=>1,'image.status'=>1,])->orderby('image.cover desc');
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'supplier_id' => Yii::t('app', 'Supplier ID'),
            'name' => Yii::t('app', 'Name'),
            'en_name' => Yii::t('app', 'En Name'),
            'from_city' => Yii::t('app', 'From City'),
            'to_city' => Yii::t('app', 'To City'),
            'weight' => Yii::t('app', 'Weight'),
            'num' => Yii::t('app', 'Num'),
            'length' => Yii::t('app', 'Length'),
            'width' => Yii::t('app', 'Width'),
            'room' => Yii::t('app', 'Room'),
            'ceil' => Yii::t('app', 'Ceil'),
            'speed' => Yii::t('app', 'Speed'),
            'first' => Yii::t('app', 'First'),
            'desc' => Yii::t('app', 'Desc'),
            'status' => Yii::t('app', 'Status'),
            'code' => Yii::t('app', 'Code'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'renovation' => Yii::t('app', 'Renovation'),
            'year' => '时间',
        ];
    }
}
